<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Seckill;
use app\common\model\SeckillGoods;
use app\common\model\FastNews;
use app\common\model\GoodsNew;
use app\common\model\GoodsHot;
use app\common\model\FixedCate;
use app\common\model\FixedCateGoods;
use app\common\model\Column;
use app\common\model\ColumnGoods;
use app\common\model\Banner;
use app\common\model\Goods;
use app\common\model\GoodsCategory;
use think\cache\driver\Redis;

class Index2 extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     */
    public function index()
    {
        // 轮播图
        $list['banner'] = (new Banner)->whereNull('deletetime')
                                        ->field(['id', 'name', 'image'])
                                        ->select();

        // 分类
        $list['category'] = (new GoodsCategory)->where('pid','=',0)
                                                ->order('weigh desc')
                                                ->select();
        foreach ($list['category'] as $row) {
            $row->visible(['id', 'name', 'image']);
        }

        // 快报
        $list['fastnews'] = (new FastNews)->where('status', '=', 2)
                                            ->field(['id', 'title', 'coverimage', 'createtime', 'maincontent'])
                                            ->order('createtime desc')
                                            ->limit(10)
                                            ->select();

        // 秒杀专场
        $nowTime = time();
        // $endToday = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        // $timeArr = [$nowTime, $endToday];
        $seckill = (new Seckill)->where('status', '=', 2)
                                ->where('endtime', '>', $nowTime)
                                // ->whereTime('endtime', 'between', $timeArr)
                                ->field(['id', 'endtime'])
                                ->order('starttime asc')
                                ->find();
        $list['seckill']['endtime'] = 0;
        $list['seckill']['surplus_at'] = 0;
        $list['seckill']['list'] = [];
        if(isset($seckill['id'])) {
            $list['seckill']['endtime'] = $seckill['endtime'] ?? 0;
            $list['seckill']['surplus_at'] = $seckill['surplus_at'] ?? 0;
            $list['seckill']['list'] = (new SeckillGoods)->with(['goods'])
                                                    // ->where('seckill_goods.status', '=', 2)
                                                    ->where('seckill_goods.seckill_id', '=', $seckill['id'])
                                                    ->where('goods.status', '=', '2')
                                                    ->whereNull('goods.deletetime')
                                                    ->order('seckill_goods.id', 'asc')
                                                    ->limit(2)
                                                    ->select();
            foreach ($list['seckill']['list'] as $row) {
                $row->visible(['id', 'goods_id']);
                $row->visible(['goods'])->getRelation('goods')->visible(['id', 'name', 'thumbnail_image', 'goods_price', 'goods_price_max']);
            }
        }
        $seckillNum = count($list['seckill']['list']);
        if($seckillNum < 2) {
            $diffNum = 2 - $seckillNum;
            $tmpArr = [];
            $newList = (new Goods)->where('status', '=', 2)
                                    ->whereNull('deletetime')
                                    ->order('recommend asc')
                                    ->limit($diffNum)
                                    ->select();

            foreach ($newList as $goods) {
                $goods->visible(['id', 'name', 'thumbnail_image', 'goods_price', 'goods_price_max']);
                $tmpArr[] = [
                    'id' => 0,
                    'goods_id' => $goods['id'],
                    'goods' => $goods
                ];
            }
            $list['seckill']['list'] = array_merge($list['seckill']['list'], $tmpArr);
        }

        // 新品尝鲜
        $list['newsgoods'] = (new GoodsNew)->with(['goods'])
                                            // ->where('goods_new.status', '=', 2)
                                            ->where('goods.status', '=', '2')
                                            ->whereNull('goods.deletetime')
                                            ->order('goods_new.weigh', 'desc')
                                            ->limit(2)
                                            ->select();
        foreach ($list['newsgoods'] as $row) {
            $row->visible(['id', 'goods_id']);
            $row->visible(['goods'])
                ->getRelation('goods')
                ->visible(['id', 'name', 'thumbnail_image', 'goods_price', 'goods_price_max']);
        }

        // 活动类别
        $list['activity'] = (new FixedCate)->select();

        // 热门商品
        $list['hotgoods'] = (new GoodsHot)->with(['goods'])
                                            // ->where('goods_hot.status', '=', 2)
                                            ->where('goods.status', '=', '2')
                                            ->whereNull('goods.deletetime')
                                            ->order('goods_hot.weigh desc')
                                            ->limit(9)
                                            ->select();
        foreach ($list['hotgoods'] as $row) {
            $row->visible(['id', 'goods_id']);
            $row->visible(['goods'])
                ->getRelation('goods')
                ->visible(['id', 'name', 'thumbnail_image', 'goods_price', 'goods_price_max']);
        }

        // 栏目类别
        $list['column'] = (new Column)->where('status', '=', 2)
                                        ->field(['id', 'name', 'sub_name', 'coverimage'])
                                        ->order('weigh', 'desc')
                                        ->select();

        $this->success('请求成功', $list);
    }

    /**
     * 获取快报列表
     */
    public function getFastnewsList()
    {
        $model = new FastNews;

        $rows = $model->where('status', '=', 2)
                      ->field(['id', 'title', 'coverimage', 'createtime', 'maincontent'])
                      ->order('id asc')
                      ->paginate();

        $this->success('请求成功', $rows);
    }

    /**
     * 获取快报信息
     */
    public function getFastnewsInfo()
    {
        $fastnewsId = $this->request->param('id/d', 0);
        if(!$fastnewsId) {
            $this->error('参数有误！');
        }

        $row = (new FastNews)->where('id', '=', $fastnewsId)
                                ->field(['id', 'title', 'coverimage', 'createtime', 'maincontent'])
                                ->find();

        if(!$row['id']) {
            $this->error('参数有误！');
        }

        $this->success('请求成功', $row);
    }

    /**
     * 展示快报内容
     */
    public function fastnewsCon()
    {
        $fastnewId = $this->request->param('id/d', 0);

        $data = FastNews::where('id', $fastnewId)->value('maincontent');

        return view('', ['data' => $data]);
    }

    /**
     * 获取秒杀列表
     */
    public function getSeckillList()
    {
        // php获取今日开始时间戳和结束时间戳
        // $beginToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
        // $endToday = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;

        $model = new Seckill;

        $nowTime = time();
        // $timeArr = [$nowTime, $endToday];
        $rows = $model->where('status', '=', 2)
                      ->where('endtime', '>', $nowTime)
                      // ->whereTime('endtime', 'between', $timeArr)
                      ->field(['id', 'name', 'starttime', 'endtime'])
                      ->order('starttime asc')
                      ->select();

        $this->success('请求成功', ['list'=>$rows]);
    }

    /**
     * 获取秒杀商品
     */
    public function getSeckillGoods()
    {
        $seckillId = $this->request->param('id/d', 0);
        if(!$seckillId) {
            $this->error('参数有误！');
        }

        $model = new SeckillGoods;

        $rows = $model->with(['goods'])
                        // ->where('seckill_goods.status', '=', 2)
                        ->where('seckill_goods.seckill_id', '=', $seckillId)
                        ->where('goods.status', '=', '2')
                        ->whereNull('goods.deletetime')
                        ->order('seckill_goods.id', 'asc')
                        ->paginate();

        foreach ($rows as $row) {
            $row->visible(['id', 'goods_id']);
            $row->visible(['goods'])->getRelation('goods')->visible(['id', 'name', 'thumbnail_image', 'goods_price', 'goods_price_max']);
        }

        $this->success('请求成功', $rows);
    }

    /**
     *  获取新品尝鲜列表
     */
    public function getNewgoodsList()
    {
        $model = new GoodsNew;

        $rows = $model->with(['goods'])
                        // ->where('goods_new.status', '=', 2)
                        ->where('goods.status', '=', '2')
                        ->whereNull('goods.deletetime')
                        ->order('goods_new.weigh', 'desc')
                        ->paginate();

        foreach ($rows as $row) {
            $row->visible(['id', 'goods_id']);
            $row->visible(['goods'])
                ->getRelation('goods')
                ->visible(['id', 'name', 'thumbnail_image', 'goods_price', 'goods_price_max']);
        }

        $this->success('请求成功', $rows);
    }

    /**
     *  获取活动商品
     */
    public function getActivityGoods()
    {
        $fixedcateId = $this->request->param('id/d', 0);
        if(!$fixedcateId) {
            $this->error('参数有误！');
        }

        $model = new FixedCateGoods;

        $rows = $model->with(['goods'])
                        // ->where('fixed_cate_goods.status', '=', 2)
                        ->where('fixed_cate_goods.cate_id', '=', $fixedcateId)
                        ->where('goods.status', '=', '2')
                        ->whereNull('goods.deletetime')
                        ->order('fixed_cate_goods.weigh', 'desc')
                        ->paginate();

        foreach ($rows as $row) {
            $row->visible(['id', 'goods_id']);
            $row->visible(['goods'])
                ->getRelation('goods')
                ->visible(['id', 'name', 'thumbnail_image', 'goods_price', 'goods_price_max']);
        }

        $cateInfo = FixedCate::find($fixedcateId);
        $rows = array_merge(($rows ? $rows->toArray() : []), ['cateinfo'=>$cateInfo]);

        $this->success('请求成功', $rows);
    }

    /**
     *  获取热门商品列表
     */
    public function getHotgoodsList()
    {
        $category1 = input('category1');
        $category2 = input('category2');
        $category3 = input('category3');
        $goods_name = input('goods_name');
        $price_min = input('price_min');
        $price_max = input('price_max');
        $recommend = input('recommend',2);
        $type = input('type');//1综合2销量低3销量高4价格低5价格高
        $cid = input('cid');//分类id
        $where = [];
        // $where['goods_hot.status'] = '2';
        if ($category1) {
            $where['goods.category_id'] = $category1;
        }
        if ($category2) {
            $where['goods.category2_id'] = $category2;
        }
        if ($category3) {
            $where['goods.category3_id'] = $category3;
        }
        if($cid){
            $where['category3_id|category_id|category2_id'] = $cid;
        }
        if ($goods_name) {
            $where['goods.name'] = ['like', "%" . $goods_name . "%"];
        }
        if ($price_min){
            $where['goods.goods_price'] = ['>', $price_min];
        }
        if ($price_max){
            $where['goods.goods_price'] = ['<', $price_max];
        }
        if ($recommend==1){
            $where['goods.recommend'] = $recommend;
        }
        // if ($price_min && $price_max) {
        //     $where['goods.goods_price'] = ['between', [$price_min, $price_max]];
        // }
        switch ($type) {
            case '1':
                $group_type = 'goods_hot.weigh desc';
                break;
            case '2':
                $group_type = 'goods.sold_number';
                break;
            case '3':
                $group_type = 'goods.sold_number desc';
                break;
            case '4':
                $group_type = 'goods.goods_price ';
                break;
            case '5':
                $group_type = 'goods.goods_price desc';
                break;
            default:
                $group_type = 'goods_hot.weigh desc';

        }

        $model = new GoodsHot;
        $rows = $model->with(['goods'])
                        ->where($where)
                        ->where('goods.status', '=', '2')
                        ->whereNull('goods.deletetime')
                        ->order($group_type)
                        ->paginate();

        foreach ($rows as $row) {
            $row->visible(['id', 'goods_id']);
            $row->visible(['goods'])
                ->getRelation('goods')
                ->visible(['id', 'name', 'thumbnail_image', 'goods_price', 'goods_price_max']);
        }

        $this->success('请求成功', $rows);
    }

    /**
     * 获取栏目列表
     */
    public function getColumnList()
    {
        $model = new Column;

        $rows = $model->where('status', '=', 2)
                        ->field(['id', 'name', 'sub_name', 'coverimage'])
                        ->order('weigh', 'desc')
                        ->select();

        $this->success('请求成功', ['list'=>$rows]);
    }

    /**
     * 获取栏目商品
     */
    public function getColumnGoods()
    {
        $columnId = $this->request->param('id/d', 0);
        if(!$columnId) {
            $this->error('参数有误！');
        }

        $banner = (new Column)->find($columnId)['coverimage'];
        $tmpArr = [];
        foreach ($banner as $value) {
            $tmpArr[] = [
                'image' => $value,
            ];
        }

        $model = new ColumnGoods;

        $rows = $model->with(['goods'])
                        // ->where('column_goods.status', '=', 2)
                        ->where('column_goods.column_id', '=', $columnId)
                        ->where('goods.status', '=', '2')
                        ->whereNull('goods.deletetime')
                        ->order('column_goods.weigh', 'desc')
                        ->paginate();

        foreach ($rows as $row) {
            $row->visible(['id', 'goods_id']);
            $row->visible(['goods'])
                ->getRelation('goods')
                ->visible(['id', 'name', 'thumbnail_image', 'goods_price', 'goods_price_max']);
        }

        $rows = array_merge($rows ? $rows->toArray() : [], ['banner' => $tmpArr]);

        $this->success('请求成功', $rows);
    }
}